package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.GlideUtils;
import com.rongshu.utils.LoginUtils;
import com.rongshu.view.discovery.activity.UserHomePageActivity;
import com.rongshu.view.userInfo.activity.PersonDetailsActivity;
import com.rongshu.view.userInfo.entity.DirectChat;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import static com.rongshu.utils.UserInfoUtils.getUserId;

public class DirectChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //context
    private Context mContext = null;
    //list
    private List<DirectChat.ResponseBean> directChatList = new ArrayList<>();
    //type
    public static final int ONESELF = 0x01;
    public static final int OTHERS = 0x02;

    public DirectChatAdapter(Context context) {
        this.mContext = context;
    }

    public void setDirectChatList(List<DirectChat.ResponseBean> list) {
        this.directChatList = list;
    }

    public void addDirectChatList(List<DirectChat.ResponseBean> list) {
        this.directChatList.addAll(list);
    }

    public List<DirectChat.ResponseBean> getDirectChatList() {
        return this.directChatList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case ONESELF:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_direct_chat_oneself_item_view, parent, false);
                return new OneSelfViewHolder(view);
            case OTHERS:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_direct_chat_others_item_view, parent, false);
                return new OthersViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        try {
            final DirectChat.ResponseBean responseBean = directChatList.get(position);
            if (holder instanceof OneSelfViewHolder) {

                OneSelfViewHolder viewHolder = (OneSelfViewHolder) holder;
                //头像
                GlideUtils.loadImageView(mContext, viewHolder.iv_oneself_icon, responseBean.getAvatar(),
                        R.drawable.user_head_icon, R.drawable.user_head_icon);
                //content
                viewHolder.txt_oneself_content.setText(URLDecoder.decode(responseBean.getContents(), "utf-8"));
                //time
                String time = responseBean.getCreate_datetime();
                viewHolder.txt_oneself_time.setText(time);
                viewHolder.iv_oneself_icon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, PersonDetailsActivity.class);
                        mContext.startActivity(intent);
                    }
                });
            } else if (holder instanceof OthersViewHolder) {
                OthersViewHolder viewHolder = (OthersViewHolder) holder;
                //头像
                GlideUtils.loadImageView(mContext, viewHolder.iv_others_icon, responseBean.getAvatar(),
                        R.drawable.user_head_icon, R.drawable.user_head_icon);
                //content
                String content = responseBean.getContents();
                content = content.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
                content = URLDecoder.decode(content, "utf-8");
                viewHolder.txt_others_content.setText(content);
                //time
                String time = responseBean.getCreate_datetime();
                viewHolder.txt_others_time.setText(time);
                viewHolder.iv_others_icon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int user_id = getUserId();
                        if (user_id == 0) {
                            LoginUtils.toLogin(mContext);
                            return;
                        }
                        Intent intent = null;
                        if (user_id == responseBean.getSend_userid()) {
                            intent = new Intent(mContext, PersonDetailsActivity.class);
                        } else {
                            intent = new Intent(mContext, UserHomePageActivity.class);
                            intent.putExtra(Constant.USER_ID, responseBean.getSend_userid());
                        }
                        mContext.startActivity(intent);
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        if (directChatList == null) {
            return 0;
        }
        return directChatList.size();
    }

    @Override
    public int getItemViewType(int position) {
        DirectChat.ResponseBean directChat = this.directChatList.get(position);
        return directChat.getType();
    }

    public static class OneSelfViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv_oneself_icon;

        private TextView txt_oneself_content, txt_oneself_time;

        public OneSelfViewHolder(View itemView) {
            super(itemView);
            iv_oneself_icon = itemView.findViewById(R.id.iv_oneself_icon);
            txt_oneself_content = itemView.findViewById(R.id.txt_oneself_content);
            txt_oneself_time = itemView.findViewById(R.id.txt_oneself_time);
        }
    }

    public static class OthersViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv_others_icon;

        private TextView txt_others_content, txt_others_time;

        public OthersViewHolder(View itemView) {
            super(itemView);
            iv_others_icon = itemView.findViewById(R.id.iv_others_icon);
            txt_others_content = itemView.findViewById(R.id.txt_others_content);
            txt_others_time = itemView.findViewById(R.id.txt_others_time);
        }
    }
}
